import { TYPES } from '../reduces/tableReducer';
import Http from '../../../utils/http';
import _ from 'lodash';
import {
  Toast,
} from 'antd-mobile-rn';
import { i18n } from '../../../i18n/index';

export const getLayoutList = () => async dispatch => {
  try {
    const http = new Http();

    dispatch({ type: TYPES.GET_LAYOUT_LIST });

    const { status, data } = await http.post('layoutList');

    if (status) {
      dispatch({ type: TYPES.GET_LAYOUT_LIST_SUCCESS, layouts: data.layouts });
    } else {
      throw new Exception();
    }
  } catch (_) {
    dispatch({ type: TYPES.GET_LAYOUT_LIST_FAIL });

    Toast.offline(i18n.t('store.table.error.getlayoutlist'), 2);
  }
};

export const createLayout = () => {
  return {
    type: TYPES.CREATE_LAYOUT,
  };
};

export const saveLayout = (layout) => async dispatch => {
  try {
    const http = new Http();

    dispatch({ type: TYPES.SAVE_LAYOUT });

    let newLayout = {
      name: layout.name,
      status: layout.status,
      sort_order: layout.sort_order,
    };
    let isAdded = true;
    if (_.has(layout, 'server_layout_id')) {
      newLayout.layout_id = layout.server_layout_id;
      isAdded = false;
    }

    const { status, data } = await http.post('layoutSave', newLayout);

    if (status) {
      const { layout_id } = data;
      newLayout.server_layout_id = layout_id;

      dispatch({ type: TYPES.SAVE_LAYOUT_SUCCESS, layout: newLayout, is_new: isAdded });
    } else {
      throw new Exception();
    }
  } catch (_) {
    dispatch({ type: TYPES.SAVE_LAYOUT_FAIL });

    Toast.offline(i18n.t('store.table.error.savelayout'), 2);
  }
};

export const editlayout = (layout) => {
  return {
    type: TYPES.EDIT_EXISTING_LAYOUT,
    layout,
  };
}

export const cancelEditLayout = () => {
  return {
    type: TYPES.CANCEL_EDIT_LAYOUT,
  };
}

export const deleteLayout = (layoutId, onSuccess) => async dispatch => {
  try {
    const http = new Http();

    dispatch({ type: TYPES.DELETE_LAYOUT });

    const { status, data } = await http.post('layoutDelete', { layout_id: layoutId });

    if (status) {
      onSuccess && onSuccess();

      dispatch({ type: TYPES.DELETE_LAYOUT_SUCCESS, layout_id: layoutId });
    } else {
      throw new Exception();
    }
  } catch (_) {
    dispatch({ type: TYPES.DELETE_LAYOUT_FAIL });

    Toast.offline(i18n.t('store.table.error.deletelayout'), 2);
  }
};

export const getDeskList = (layoutId) => async dispatch => {
  try {
    const http = new Http();

    dispatch({ type: TYPES.GET_DESK_LIST });

    const { status, data } = await http.post('deskList', { layout_id: layoutId });

    if (status) {
      dispatch({ type: TYPES.GET_DESK_LIST_SUCCESS, desks: data.desks });
    } else {
      throw new Exception();
    }
  } catch (_) {
    dispatch({ type: TYPES.GET_DESK_LIST_FAIL });

    Toast.offline(i18n.t('store.table.error.getdesklist'), 2);
  }
};

export const addDesk = (layoutId) => {
  return {
    type: TYPES.ADD_DESK,
    layout_id: layoutId,
  };
};

export const cancelEditDesk = () => {
  return {
    type: TYPES.CANCEL_EDIT_DESK,
  };
}

export const saveDesk = (layoutId, originDesks, desk, onSuccess) => async dispatch => {
  try {
    const http = new Http();

    dispatch({ type: TYPES.SAVE_DESK });

    let isAdded = true;
    if (_.has(desk, 'server_desk_id')) {
      isAdded = false;
    }

    let newDesks = originDesks.map((item) => {
      let newItem = {
        name: item.name,
        sort_order: item.sort_order,
        status: item.status,
        desk_id: item.server_desk_id,
        position: {
          x: item.x,
          y: item.y,
        },
      };
      delete newItem['server_desk_id'];

      if (newItem.desk_id === desk.server_desk_id) {
        newItem = {
          ...newItem,
          name: desk.name,
          position: {
            x: desk.x,
            y: desk.y,
          },
        }
      }

      return newItem;
    });

    if (isAdded) {
      newDesks = [...newDesks, desk];
    }

    const postData = {
      layout_id: layoutId,
      desks: newDesks,
    };

    const { status, data } = await http.post('saveDesks', {
      data: JSON.stringify(postData),
    });

    if (status) {
      const { success } = data;

      let newDeskId = desk.server_desk_id;
      let newDesk = {
        ...desk,
        x: desk.position.x,
        y: desk.position.y,
      };
      if (isAdded) {
        newDeskId = success.slice(-1)[0];
        newDesk.server_desk_id = newDeskId;
      }
      delete newDesk['position'];

      onSuccess && onSuccess(newDeskId);

      dispatch({ type: TYPES.SAVE_DESK_SUCCESS, desk: newDesk, is_new: isAdded });
    } else {
      throw new Exception();
    }
  } catch (_) {
    dispatch({ type: TYPES.SAVE_DESK_FAIL });

    Toast.offline(i18n.t('store.table.error.savedesks'), 2);
  }
};

export const deleteDesk = (deskId, onSuccess) => async dispatch => {
  try {
    const http = new Http();

    dispatch({ type: TYPES.DELETE_DESK });

    const { status, data } = await http.post('deskDelete', { desk_id: deskId });

    if (status) {
      onSuccess && onSuccess(deskId);

      dispatch({ type: TYPES.DELETE_DESK_SUCCESS, desk_id: deskId });
    } else {
      throw new Exception();
    }
  } catch (_) {
    dispatch({ type: TYPES.DELETE_DESK_FAIL });

    Toast.offline(i18n.t('store.table.error.deletedesk'), 2);
  }
};

export const editDesk = (desk) => {
  let newDesk = {
    ...desk,
    position: {
      x: desk.x,
      y: desk.y,
    }
  };

  return {
    type: TYPES.EDIT_EXISTING_DESK,
    desk: newDesk,
  };
}

export const saveSeats = (deskId, seats, onSuccess) => async dispatch => {
  try {
    const http = new Http();

    dispatch({ type: TYPES.SAVE_SEATS });

    const postData = {
      desk_id: deskId,
      seats: seats,
    };

    const { status, data } = await http.post('saveSeats', {
      data: JSON.stringify(postData),
    });

    if (status) {
      const newSeats = seats.map(seat => {
        return {
          x: seat.position.x,
          y: seat.position.y,
        };
      });

      onSuccess && onSuccess(newSeats);

      dispatch({ type: TYPES.SAVE_SEATS_SUCCESS, desk_id: deskId, seats: newSeats });
    } else {
      throw new Exception();
    }
  } catch (_) {
    dispatch({ type: TYPES.SAVE_SEATS_FAIL });

    Toast.offline(i18n.t('store.table.error.saveseats'), 2);
  }
};
